import {
  userJobList,
  userJobResumeList,
  userResumeAdd,
  userResumeSend,
  addResearch,
  userJobAdd,
  userInSchoolAdd,
  userHonorAdd,
  userResumeUpdate,
  chatList
} from './index';
import {
  JobInteractionListParams,
  commonJobInteractionList
} from './common';
// import home06 from "@/static/images/tab/home-06.png";
// import home10 from "@/static/images/tab/home-10.png";
const home06 = 'https://yiliao.test03.qcw800.com/static/images/tab/home-06.png'
const home10 = 'https://yiliao.test03.qcw800.com/static/images/tab/home-10.png'

/**
 * 用户-职位列表
 * @param {any} params status position_id salary_min salary_max province_id city_id qualifications company_id
 * @param {string} fromContent 来源内容(打印日志用)
 * @return {any} {
    likeCount: 0,
    jobList: [{
      job_name: '',
      job_id: 0,
      salary_min: 0,
      salary_max: 0,
      salary_count: 0,
      province: '',
      city: '',
      job_nature: '',
      qualifications: '',
      highlights: [],
      company_id: '',
      company_logo: '',
      company_name: '',
      company_type: '',
      company_listed: '',
      like: '',
      like_img: '',
      hr_avatar: '',
      hr_name: '',
      hr_duty: '',
      hr_id: '',
    }]
  }
 */
export async function customerJobList(params: any, fromContent: string="未知"): Promise<any> {
  //入参
  let data : any = {
  }
  if (!params) {
    console.log(fromContent,'用户-职位列表 请求参数为空')
    return false
  }
  if (params.status) {  //职位状态 1招聘中 2已关闭 3未过审
    data.status = params.status
  }
  if (params.position_id && params.position_id > 0) {   //职位ID
    data.position_id = params.position_id
  }
  if (params.salary_min) {
    data.salary_min = params.salary_min
  }
  if (params.salary_max) {
    data.salary_max = params.salary_max
  }
  if (params.province_id) {
    data.province_id = params.province_id
  }
  if (params.city_id) {
    data.city_id = params.city_id
  }
  if (params.qualifications) {
    data.qualifications = params.qualifications
  }
  if (params.company_id) {
    data.company_id = params.company_id
  }
  if (params.job_name) {
    data.job_name = params.job_name
  }
  let resultObj = {
    likeCount: 0,
    jobList: []
  }
  try{
    let result = await userJobList(params);
    console.log(fromContent, ' 用户-职位列表 r:::', result);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!result) {
      console.log(fromContent,'用户-职位列表 返回值为空')
      return
    }
    if (result.like_count) {
      resultObj.likeCount = result.like_count;
    }
    for (let i = 0; i < result.list.length; i++) {
      const r = result.list[i]
      let rr = {
        job_name: r.name,
        job_id: r.id,
        salary_min: r.salary_min,
        salary_max: r.salary_max,
        salary_count: r.salary_count,
        province: r.province.name,
        city: r.city.name,
        job_nature: r.nature,
        qualifications: '',
        highlights: r.highlights,
        company_id: r.company.id,
        company_logo: r.company.url_logo,
        company_name: r.company.name,
        company_type: r.company.type == 1 ? '民营' : r.company.type == 2 ? '国企' : '合资',
        company_listed: r.company.listed == 1 ? '未上市' : '上市',
        like: r.like,
        like_img: r.like == 1 ? home06 : home10,
        hr_avatar: r.user.avatar,
        hr_name: r.user.name,
        hr_duty: r.user.hr.duty.name,
        hr_id: r.user.id,
      }
      let qualifications = ''
      for (let j = 0; j < r.qualifications.length; j++) {
        qualifications += r.qualifications[j] + ' '
      }
      rr.qualifications = qualifications
      resultObj.jobList.push(rr)
    }
    console.log(fromContent, ' 用户-职位列表转义结果 r:::', resultObj);
    return resultObj;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 请求用户-职位列表异常 err:::', e)
    return false
  }
}

export async function customerJobResumeList(params: any, fromContent: string="未知"): Promise<any> {
  //入参
  let data : any = {
  }
  if (!params) {
    console.log(fromContent,'用户-职位列表 请求参数为空')
    return false
  }
  if (params.status) {  //投递状态 1待处理 2已联系 3不合适 4通过初筛 5通过初试 6通过复试 7通过终试 8已签约
    data.status = params.status
  }
  if (params.status_recommend) {
    data.status_recommend = params.status_recommend
  }
  let resultObj = {
    jobResumeList: []
  }
  try{
    let result = await userJobResumeList(params);
    console.log(fromContent, ' 用户-投递简历列表 r:::', result);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!result) {
      console.log(fromContent,'用户-投递简历列表 返回值为空')
      return
    }
    for (let i = 0; i < result.list.length; i++) {
      const r = result.list[i]
      let rr = {
        job_name: r.job.name,
        job_id: r.job.id,
        salary_min: r.job.salary_min,
        salary_max: r.job.salary_max,
        salary_count: r.job.salary_count,
        province: r.job.province.name,
        city: r.job.city.name,
        job_nature: r.job.nature,
        qualifications: '',
        highlights: r.job.highlights,
        company_id: r.company.id,
        company_logo: r.company.url_logo,
        company_name: r.company.name,
        company_type: r.company.type == 1 ? '民营': r.job.company.type == 2 ? '国企' : '合资',
        company_listed: r.company.listed == 1 ? '未上市': '上市',
        like: r.like,
        like_img: r.like == 1 ? home06 : home10,
        hr_avatar: r.user.avatar,
        hr_name: r.user.name,
        hr_duty: r.user.hr.duty.name,
        hr_id: r.user.id,
      }
      let qualifications = ''
      for (let j = 0; j < r.job.qualifications.length; j++) {
        qualifications += r.job.qualifications[j] + ' '
      }
      rr.qualifications = qualifications
      resultObj.jobResumeList.push(rr)
    }
    console.log(fromContent, ' 用户-投递简历列表转义结果 r:::', resultObj);
    return resultObj;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-投递简历列表异常 err:::', e)
    return false
  }
}

export interface ResumeAddResult {
  id: number;
  intention: IntentionResult;
  userInfo: UserInfoResult;
  educationList: EducationResult[];
}
export interface IntentionResult {
  id: number;
  nature: string;
  province: string;
  city: string;
  position_type: string;
  position: string;
  salary_min: number;
  salary_max: number;
}
export interface UserInfoResult {
  id: number;
  name: string;
  avatar: string;
  gender: string;
  age: number;
  political_outlook: number;
  phone: string;
  email: string;
  province: string;
  city: string;
  qualification?: string;
  nature?: string,
}
export interface EducationResult {
  id: number;
  school_name: string;
  time: string;
  college_name: string;
  qualification: string;
  majors_name: string;
  status_overseas_study: string;
}
/**
 * 用户新增简历
 * @param {string} fromContent
 * @return {ResumeAddResult}
 */
export async function customerResumeAdd(fromContent: string="未知"): Promise<ResumeAddResult> {
  let resultObj:ResumeAddResult = {
    id: 0,
    intention: {
      id: 0,
      nature: '',
      province: '',
      city: '',
      position_type: '',
      position: '',
      salary_min: 0,
      salary_max: 0,
    },
    userInfo: {
      id: 0,
      name: '',
      avatar: '',
      gender: '',
      age: 0,
      political_outlook: 1,
      phone: '',
      email: '',
      province: '',
      city: ''
    },
    educationList: []

  }
  try{
    let r = await userResumeAdd();
    console.log(fromContent, ' 用户-简历新增 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r) {
      console.log(fromContent,'用户-简历新增 返回值为空')
      return resultObj;
    }
    resultObj.id = r.id
    if (resultObj.intention) {
      resultObj.intention.id = r.intention.id
      resultObj.intention.nature = r.intention.nature
      resultObj.intention.province = r.intention.province.name
      resultObj.intention.city = r.intention.city.name
      resultObj.intention.position_type = r.intention.position.p_content
      resultObj.intention.position = r.intention.position.content
      resultObj.intention.salary_min = r.intention.salary_min
      resultObj.intention.salary_max = r.intention.salary_max
    }
    resultObj.userInfo.name = r.user.name
    resultObj.userInfo.avatar = r.user.avatar
    resultObj.userInfo.gender = r.user.gender
    resultObj.userInfo.age = r.user.age
    resultObj.userInfo.phone = r.user.phone
    resultObj.userInfo.email = r.user.email
    resultObj.userInfo.province = r.user.province.name
    resultObj.userInfo.city = r.user.city.name

    for (let i = 0; i < r.education.length; i++) {
      const e = r.education[i]
      let rr = {
        id: e.id,
        school_name: e.school_name,
        time: e.time,
        college_name: e.college_name,
        qualification: e.qualification,
        majors_name: e.majors_name,
        status_overseas_study: e.status_overseas_study == 1 ? '留学' : '未留学'
      }
      resultObj.educationList.push(rr)
    }
    console.log(fromContent, ' 用户-简历新增转义结果 r:::', resultObj);
    return resultObj;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-简历新增异常 err:::', e)
    return resultObj;
  }
}

export interface ResumeUpdateParams {
  id: number;
  intention_id?: number;
  highlights?: string;
  skill_certificate_content?: string;
  heart_content?: string;
  remarks?: string;
}
export async function customerResumeUpdate(params:ResumeUpdateParams, fromContent: string="未知"): Promise<boolean> {
  try{
    let r = await userResumeUpdate(params);
    console.log(fromContent, ' 用户-更新简历结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r || !r.id) {
      console.log(fromContent,'用户-更新简历 返回值异常')
      return false;
    }
    return true;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-更新简历异常 err:::', e)
    return false;
  }
}

export interface ScientificParams {
  time: string;
  research_direction: string;
  research_subject: string;
  mentor_name: string;
  mentor_phone: number;
  mentor_info: string;
  subject_info: string; //课题描述
  paper_name: string; //论文名字
  author_order: string; //作者顺序
  paper_status: string; //发表状态
  academic_journals: string;  //学术期刊
  journals_type: string;  //期刊分类
  resume_id: number;   //简历ID
  education_id: number; //教育经历ID
}
/**
 * 用户-发送简历
 * @param {any} params job_id resume_id
 * @param {string} fromContent
 * @return {boolean}
 */
export async function customerResumeSend(params:any, fromContent: string="未知"): Promise<boolean> {
  try{
    //入参
    if (!params) {
      console.log(fromContent,'用户-发送简历 请求参数为空')
      return false
    }
    let data = {
      job_id: params.job_id,
      resume_id: params.resume_id
    }
    let r = await userResumeSend(data);
    console.log(fromContent, ' 用户-发送简历结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r) {
      console.log(fromContent,'用户-发送简历 返回值为空')
      return false;
    }
    return true;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-简历新增异常 err:::', e)
    return false;
  }
}

export interface ScientificParams {
  time: string;
  research_direction: string;
  research_subject: string;
  mentor_name: string;
  mentor_phone: string;
  mentor_info: string;
  subject_info: string; //课题描述
  paper_name: string; //论文名字
  author_order: string; //作者顺序
  paper_status: string; //发表状态
  academic_journals: string;  //学术期刊
  journals_type: string;  //期刊分类
  resume_id: string;   //简历ID
  education_id: string; //教育经历ID
}
/**
 * 用户-新增科研经历
 * @param {any} params
 * @param {string} fromContent
 * @return {number} 0 或 id值
 */
export async function customerScientificAdd(params:ScientificParams, fromContent: string="未知"): Promise<number> {
  try{
    let r = await addResearch(params);
    console.log(fromContent, ' 用户-新增科研经历结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r || !r.id) {
      console.log(fromContent,'用户-新增科研经历 返回值为空')
      return 0;
    }
    return r.id;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-新增科研经历异常 err:::', e)
    return 0;
  }
}

export interface JobOrInSchoolParams {
  time: string;
  department: string; //实习/在校 部门
  position: string; //实习/在校 职务
  content: string;  //实习/在校 内容
  resume_id: number;   //简历ID
}
/**
 * 用户-新增实习经历
 * @param {JobOrInSchoolParams} params
 * @param {string} fromContent
 * @return {number} 0 或 id值
 */
export async function customerJobAdd(params:JobOrInSchoolParams, fromContent: string="未知"): Promise<number> {
  try{
    let r = await userJobAdd(params);
    console.log(fromContent, ' 用户-新增实习经历结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r || !r.id) {
      console.log(fromContent,'用户-新增实习经历 返回值为空')
      return 0;
    }
    return r.id;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-新增实习经历异常 err:::', e)
    return 0;
  }
}

/**
 * 用户-新增在校经历
 * @param {JobOrInSchoolParams} params
 * @param {string} fromContent
 * @return {number} 0 或 id值
 */
export async function customerInSchoolAdd(params:JobOrInSchoolParams, fromContent: string="未知"): Promise<number> {
  try{
    let r = await userInSchoolAdd(params);
    console.log(fromContent, ' 用户-新增在校经历结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r || !r.id) {
      console.log(fromContent,'用户-新增在校经历 返回值为空')
      return 0;
    }
    return r.id;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-新增在校经历异常 err:::', e)
    return 0;
  }
}

export interface HonorParams {
  time: string;
  name: string; //荣誉名称
  resume_id: number;   //简历ID
}
/**
 * 用户-新增荣誉经历
 * @param {HonorParams} params
 * @param {string} fromContent
 * @return {number} 0 或 id值
 */
export async function customerHonorAdd(params:HonorParams, fromContent: string="未知"): Promise<number> {
  try{
    let r = await userHonorAdd(params);
    console.log(fromContent, ' 用户-新增荣誉经历结果 r:::', r);
    //vue3传递result的值到子组件JobList中的属性jobList中
    if (!r || !r.id) {
      console.log(fromContent,'用户-新增荣誉经历 返回值为空')
      return 0;
    }
    return r.id;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, ' 用户-新增荣誉经历异常 err:::', e)
    return 0;
  }
}

export interface TalkNewReturn {
  user_id: number;
  resume_send: number;
  name: string;
  avatar: string;
  detail_content: string;
}
export async function userTalkNew(fromContent: string="用户新消息"):Promise<TalkNewReturn[]> {
  let resultObj:TalkNewReturn[] = []
  try{
    let params:JobInteractionListParams = {
      talk_hr: 2,
      talk: 1
    }
    let r = await chatList(params)
    console.log(fromContent, ' 聊天列表list 结果:::', r)
    if (!r.list) {
      return resultObj;
    }
    r.list.forEach(item => {
       // let rr:TalkNewReturn = {
       //   user_id: item.user_id,
       //   resume_send: item.resume_send,
       //   name: item.job.user.name,
       //   avatar: item.job.user.avatar,
       //   detail_content: "未知公司"
       // }
       // if (item.job && item.job.company && item.job.company.name) {
       //   rr.detail_content = item.job.company.name
       // }
      item.options = [
        {
          text: '置顶',
          style: {
            backgroundColor: '#afb2bb',
          },
        },
        {
          text: '删除',
          style: {
            backgroundColor: '#fc4749',
          },
        },
      ]
       // resultObj.push(item)
    })
    console.log(fromContent, '聊天列表list 转义结果', resultObj);
    return r;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, '聊天列表异常 err:::', e)
    return resultObj;
  }
}

export async function userTalkING(fromContent: string="用户沟通中"):Promise<TalkNewReturn[]> {
  let resultObj:TalkNewReturn[] = []
  try{
    let params:JobInteractionListParams = {
      talk_hr: 1
    }
    let r = await chatList(params)
    console.log(fromContent, ' 聊天列表list 结果:::', r)
    if (!r.list) {
      return resultObj;
    }
    r.list.forEach(item => {
       let rr:TalkNewReturn = {
         user_id: item.user_id,
        resume_send: item.resume_send
       }
       resultObj.push(rr)
    })
    console.log(fromContent, '聊天列表list 转义结果', resultObj);
    return resultObj;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, '聊天列表异常 err:::', e)
    return resultObj;
  }
}

export async function userTalkLike(fromContent: string="用户感兴趣 "):Promise<TalkNewReturn[]> {
  let resultObj:TalkNewReturn[] = []
  try{
    let params:JobInteractionListParams = {
      like_hr: 1
    }
    let r = await chatList(params)
    console.log(fromContent, '聊天列表list 结果:::', r)
    if (!r.list) {
      return resultObj;
    }
    r.list.forEach(item => {
       let rr:TalkNewReturn = {
         user_id: item.user_id,
        resume_send: item.resume_send
       }
       resultObj.push(rr)
    })
    console.log(fromContent, '聊天列表list 转义结果', resultObj);
    return resultObj;
  }catch(e){
    //TODO handle the exception
    console.log(fromContent, '聊天列表异常 err:::', e)
    return resultObj;
  }
}
